<?
class Application_View_Helper_Tr extends Zend_View_Helper_Abstract
{
    public function tr($value = null)
    {
        global $translate_params;        
        $lang_id = Zend_Controller_Front::getInstance()->getRequest()->getParam('lang', 1);        
	if(!empty($value))
        {
            $translate_st = Doctrine_Core::getTable('Default_Model_TranslateSt');
            $key_hash = $translate_st->crypt($value);
            $controller = $translate_st->generateControllerName();
            if($controller != null)
            {    
                if($translate_params == null)
                {
                    $rows = $translate_st->getRows($lang_id,$controller);
                    $translate_params['data'] = $rows;
                    $lang = Doctrine_Core::getTable('Default_Model_Language');
                    $source_lang = $lang->getSourceLang();
                    $translate_params['source_lang'] = $source_lang;
                }
                else
                {
                    $rows = $translate_params['data'];
                    $source_lang = $translate_params['source_lang'];
                }     
                //Check Source Language
                $r3 = $translate_st->getRows($source_lang);
                if(!isset($r3[$key_hash]))
                {
                    $translate_st->addRow($source_lang,$controller,$key_hash,$value);    
                }
                
                if(isset($rows[$key_hash]))
                {
                    $result = $rows[$key_hash]['value'];
                }
                else
                {
                    $r2 = $translate_st->getRows($lang_id);
                    if(isset($r2[$key_hash]))
                    {
                        $translate_st->addRow($lang_id,$controller,$key_hash,$r2[$key_hash]['value']);
                        $result = $r2[$key_hash]['value'];
                    }
                    else
                    {
                        $translate_st->addRow($lang_id,$controller,$key_hash,$value);
                        $result = $value;
                    }                    
                }
                
            }
            else
            {
                $r2 = $translate_st->getRows($lang_id);
                if(isset($r2[$key_hash]))
                {
                    $result = $r2[$key_hash]['value'];
                }
                else
                {
                    $result = $value;
                }
            }
        }
        else
        {
            $result = '';
        }
        $translator = 0;
        
        if(!empty($translator))
        {
            return "<a href='#' onClick=\"quick_translate('" . $key_hash . "','" . $controller . "');return false;\">" . $result . "</a>";
        }
        else
        {
            return $result;
        }
    }
}
?>